package com.example.views;

import com.example.R;
import com.example.model.UserList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class AdminActivity extends Activity {

	private String adminName;

	/**
	 * Creates the activity and displays the correct information to the user.
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_admin);

		Intent intent = getIntent();
		adminName = intent.getStringExtra(MainActivity.USERNAME);

		ListView userList = (ListView) findViewById(R.id.myList);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				R.layout.simplerow, UserList.getUsernameList());
		userList.setAdapter(adapter);

		userList.setOnItemClickListener(new UserListListener());
	}

	/**
	 * Fills up the options menu
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * Handles what happens when someone presses the back button
	 * 
	 * @param view
	 */
	public void back(View view) {
		Intent intent = new Intent(this, ProfileActivity.class);
		intent.putExtra(MainActivity.USERNAME, adminName);
		startActivity(intent);
	}

	/**
	 * Detects a user click on a list item, then sends the user to a view of the
	 * chosen item's information.
	 */
	private class UserListListener implements OnItemClickListener {
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			String username = ((TextView) view).getText().toString();
			Intent intent = new Intent(getApplicationContext(),
					AdminUserActivity.class);
			intent.putExtra("USERNAME_TO_ADMIN", username);
			intent.putExtra(MainActivity.USERNAME, adminName);
			startActivity(intent);
		}
	}
}